﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Exercise1Project.Models; // added namespace along with added reference to main project
using System.Data;

namespace UnitTestBikes.Models
{
    class MockBikeRepository : IBikeRepository
    {
        private List<Bike> _db = new List<Bike>();          // in memory list of bikes belonging to all customers (for tests only)

        public Exception ExceptionToThrow { get; set; }

       public IEnumerable<Bike> GetBikeByCustomerID(string id)  // get all the bikes belonging to one customer only
        {
            return _db.Where(d => d.CustomerID == id);
        }

       public Bike GetBikeByBikeId(int id)                    // get a particurlar customers particurlar bike 
       {
           return _db.FirstOrDefault(d => d.BikeID == id);
       }

       public int SaveChanges()                               // dummy (does nothing)  method just to satisfy the implementation of the bke repository
       {
           return 1;
       }

       public void SaveChanges(Bike bikeToUpdate)            // only used to update a bike details in the in memory test list
       {
           foreach (Bike bike in _db)
           {
               if (bike.BikeID == bikeToUpdate.BikeID)
               {
                   _db.Remove(bike);
                   _db.Add(bikeToUpdate);
               }
           }
       }

       public Bike AddCustomerBike(Bike bikeToAdd)             // add a bike for a particurlar customer  (ie. create bike)             
       {
           if (ExceptionToThrow != null)
               throw ExceptionToThrow;
           _db.Add(bikeToAdd);
           return bikeToAdd;
       }

       public Bike UpdateBikeDetail(Bike bikeToUpdate) // Edit action or update of crud
       {        
           SaveChanges(bikeToUpdate);   // if found in list deletes old version and adds new version see this method 
           return bikeToUpdate;
       }

        public void RemoveCustomerBike(int id)                 // delete a customers bike from the list of bikes
       {
           _db.Remove(GetBikeByBikeId(id));
       }


    }// close Class

} // close namespace
